﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-01-31 13:08:07Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace Outcoder.UI.Xaml
{
	/// <summary>
	/// Extension methods for <see cref="Collection{ResourceDictionary}"/>.
	/// </summary>
	public static class ResourceDictionaryExtensions
	{
		public static void Add(this Collection<ResourceDictionary> target, 
		                       string path, string assemblyFullName)
		{
			string dictionaryName = string.Format("/{0};component/{1}", assemblyFullName, path);
			var uri = new Uri(dictionaryName, UriKind.Relative);

			/* Silverlight */
//			Application.LoadComponent(Application.Current, uri);
//			target.Add(resourceDictionary);
#if !SILVERLIGHT
			var dictionary = (ResourceDictionary)Application.LoadComponent(uri);
			target.Add(dictionary);
#else
			throw new NotImplementedException();
#endif
		}

#if !SILVERLIGHT
		/// <summary>
		/// Adds the dictionary whose location is specified by the relative pack URI.
		/// An example of such a URI 
		/// is /Outcoder.Calcium.Client;component/Gui/Controls/SplashScreen/SplashScreenView.xaml
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUrl">The relative URL.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if the specified URL is not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUrl is <c>null</c>.</exception>
		public static void AddRelativeDictionary(this Collection<ResourceDictionary> target, 
			string relativeUrl)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUrl, "relativeUrl");

			var uri = new Uri(relativeUrl, UriKind.Relative);
			var dictionary = (ResourceDictionary)Application.LoadComponent(uri);
			target.Add(dictionary);
		}

		/// <summary>
		/// Adds dictionaries whose locations are specified by relative pack URIs.
		/// An example of such a URI 
		/// is /Outcoder.Calcium.Client;component/Gui/Controls/SplashScreen/SplashScreenView.xaml
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUrls">The relative URLs of the resource dictionaries.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if any of the specified URLs are not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUrls is <c>null</c>.</exception>
		public static void AddRelativeDictionaries(this Collection<ResourceDictionary> target, 
			IEnumerable<string> relativeUrls)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUrls, "relativeUrls");

			foreach (var relativeUrl in relativeUrls)
			{
				target.AddRelativeDictionary(relativeUrl);
			}
		}

		/// <summary>
		/// Adds the dictionary whose location is specified by the relative pack URI.
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUri">The relative Uri.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if the specified URI is not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUri is <c>null</c>.</exception>
		public static void AddRelativeDictionary(this Collection<ResourceDictionary> target,
			Uri relativeUri)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUri, "relativeUri");

			var dictionary = (ResourceDictionary)Application.LoadComponent(relativeUri);
			target.Add(dictionary);
		}

		/// <summary>
		/// Adds dictionaries whose locations are specified by relative pack URIs.
		/// </summary>
		/// <param name="target">The merged dictionary to add to.</param>
		/// <param name="relativeUris">The relative URIs of the resource dictionaries.</param>
		/// <exception cref="ArgumentException">
		/// Occurs if any of the specified URIs are not relative.</exception>
		/// <exception cref="ArgumentNullException">Occurs if either the target, 
		/// or relativeUris is <c>null</c>.</exception>
		public static void AddRelativeDictionaries(this Collection<ResourceDictionary> target,
			IEnumerable<Uri> relativeUris)
		{
			ArgumentValidator.AssertNotNull(target, "target");
			ArgumentValidator.AssertNotNull(relativeUris, "relativeUrls");

			foreach (var uri in relativeUris)
			{
				target.AddRelativeDictionary(uri);
			}
		}
#endif
	}
}